package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.ShSchoolTeacherMapper;
import com.smart.campus.dto.SysUser;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.ShSchoolTeacherService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: smart-campus
 * @BelongsPackage: com.smart.campus.service.impl
 * @Author: Y S Z
 * @CreateTime: 2024-09-26  15:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class ShSchoolTeacherServiceImpl implements ShSchoolTeacherService {
    @Autowired
    private ShSchoolTeacherMapper shSchoolTeacherMapper;

    @Override
    public List<SysUser> findSchoolTeacherList(SysUser sysUser) {
        return shSchoolTeacherMapper.selectList(null);
    }

    @Override
    public PagedResponse<SysUser> pageSchoolTeacher(SysUser sysUser, Integer pageNum, Integer pageSize) {
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        IPage<SysUser> sysUserIPage = shSchoolTeacherMapper.pageSchoolTeacher(sysUser, page);
        return PaginationUtil.getPaginationResponse(sysUserIPage.getRecords(),pageNum,pageSize,sysUserIPage.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
    public void addSchoolTeacher(SysUser sysUser) {
        sysUser.setDeptId(9L);
        sysUser.setRoleId(9L);
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        shSchoolTeacherMapper.insert(sysUser);

        Long userId = sysUser.getUserId();
        Long deptId = sysUser.getDeptId();
        Long roleId = sysUser.getRoleId();

        Map<String,Long> map = new HashMap();
        map.put("userId",userId);
        map.put("deptId",deptId);
        shSchoolTeacherMapper.insertUserIdAndDeptId(map);


        Map<String,Long> map1 = new HashMap<>();
        map1.put("userId",userId);
        map1.put("roleId",roleId);
        shSchoolTeacherMapper.insertUserIdAndRoleId(map1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void delSchoolTeacher(Long userId) {
        shSchoolTeacherMapper.deleteById(userId);
        shSchoolTeacherMapper.deleteUserIdAndDeptId(userId);
        shSchoolTeacherMapper.deleteUserIdAndRoleId(userId);
    }

    @Override
    public SysUser findSchoolTeacherId(Long userId) {
        return shSchoolTeacherMapper.selectById(userId);
    }

    @Override
    public void updSchoolTeacher(SysUser sysUser) {
        shSchoolTeacherMapper.updateById(sysUser);
    }
}
